package com.jiazhong.mingxing.project.bookstore.front.listener;

import com.jiazhong.mingxing.project.bookstore.commons.bean.Order;
import com.jiazhong.mingxing.project.bookstore.commons.mapper.OrderMapper;
import com.jiazhong.mingxing.project.bookstore.front.service.impl.ShoppingCartServiceImpl;
import jakarta.annotation.Resource;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(consumerGroup = "ab123", topic = "order", selectorExpression = "success", messageModel = MessageModel.BROADCASTING)
public class ClearCartListener implements RocketMQListener<String> {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void onMessage(String s) {
        String orderId = s;
        Order order = orderMapper.selectById(orderId);
        String userId = order.getUserId();
        stringRedisTemplate.delete(ShoppingCartServiceImpl.SHOPPING_CART + userId);

    }
}
